// @ts-nocheck
import {axios_request} from "@/api/milliaAxios";
import {ElMessage} from "element-plus";
import {ref} from "vue";


export function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// icon图标显示v-show
export const phoneIcon = ref(false);
export const pwdIcon = ref(false);
export const codeIcon = ref(false);

// 显示密码
export const re_pwdVisible = ref(false);
export const re_eyeVisible = ref(false);

export const pwdSwitch = () => {
    re_pwdVisible.value = !re_pwdVisible.value
}

// 显示小眼睛
export const eyeSwitch = async () => {
    await sleep(300);
    re_eyeVisible.value = true
}


// 校验手机号
export const check_telephone = () => {
    const reg = /^1[3-9]\d{9}$/;
    if(!reg.test(registerForm.telephone) || registerForm.telephone.length > 11) {
        phoneIcon.value = false;
        return ElMessage.warning('手机号码格式不正确')
    }else {
        phoneIcon.value = true;
        console.log(phoneIcon.value)
    }
}


// 校验密码：以字母开头，长度在6~18之间，只能包含字母、数字和特殊字符  _.*#/
export const check_pwd = () => {
    re_eyeVisible.value = false;
    const reg = /^[A-Za-z][A-Za-z0-9_.*#/]{5,17}$/;
    if(!reg.test(registerForm.password) || registerForm.password.length > 16) {
        pwdIcon.value = false;
        return ElMessage.warning('密码8-16位，以字母开头，长度在6~18之间，只能包含字母、数字和特殊字符 _.*#/')
    }else {
        pwdIcon.value = true;
        console.log(pwdIcon.value)
    }
}

// 校验验证码
export const check_code = () => {
    const reg = /^[0-9]{4}/;
    if(!reg.test(registerForm.code) || registerForm.code.length > 4) {
        codeIcon.value = false;
        return ElMessage.warning('验证码格式错误，请核对后输入')
    }else{
        codeIcon.value = true;
        console.log(codeIcon.value)
    }
}

// 注册按钮功能
export const registerForm = {
    telephone: "",
    password: "",
    code: "",
}

export function userRegister(){
    return async () => {
        if (!phoneIcon.value || !pwdIcon.value || !codeIcon.value) {
            return ElMessage.warning('注册失败：请检查数据格式是否符合规范')
        } else {
            const res = await axios_request('/api/user/register/', JSON.stringify(registerForm), 'post');
            console.log("POST请求返回的数据：" + res);
            console.log(res);
            if (res.status == 200) {
                ElMessage.success(res.data.msg)
                // 页面刷新进入登录页面
                await sleep(1000);
                window.location.reload();
            } else {
                ElMessage.warning('注册失败：' + res.data.msg)
            }
        }
    };
}
